package com.example.xuanke.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xuanke.Entity.CourseEntity;
import com.example.xuanke.mapper.CourseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CourseService extends ServiceImpl<CourseMapper, CourseEntity> {
    
    /**
     * 更新课程信息，使用REQUIRES_NEW传播行为
     * 这确保即使调用者事务回滚，这个更新也会提交
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public boolean updateCourseEnrollment(Long courseId, int enrollment) {
        CourseEntity course = this.getById(courseId);
        if (course == null) {
            return false;
        }
        course.setCurrentEnrollment(enrollment);
        return this.updateById(course);
    }
} 